package com.example.goodpet.home.homeFocus.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.DateUtils;
import com.example.goodpet.utils.UIUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class HomeFocusFragmentAdapter extends RecyclerView.Adapter<HomeFocusFragmentAdapter.ViewHolder> {
    private List<UserPetInfo> lists;
    private Context context;

    public HomeFocusFragmentAdapter(List<UserPetInfo> lists,Context context) {
        this.lists = lists;
        this.context=context;
    }

    @NonNull
    @Override
    public HomeFocusFragmentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend_recycle_view, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeFocusFragmentAdapter.ViewHolder holder, int position) {
        UserPetInfo userPetInfo = lists.get(position);
        holder.tv_name.setText(userPetInfo.getOverview());
        Glide.with(UIUtils.getContext())
                .load(Constants.BASE_URL_IMAGE+userPetInfo.getPetImage().get(0))
                .into(holder.iv_recommend);

        //根据info的数据初始化发布者信息
        Glide.with(UIUtils.getContext())
                .load(Constants.BASE_URL_IMAGE+userPetInfo.getPortrait())
                .into(holder.iv_portrait);
        holder.tv_nickname.setText(userPetInfo.getNickname());
        holder.tv_postdate.setText("发布于"+ DateUtils.getDiff(DateUtils.getDate(userPetInfo.getPostdate()),DateUtils.getNowDate())+"天前");
        holder.ly_item_recommend_recycle_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UIUtils.getContext(), PetInfoActivity.class);
                intent.putExtra("userPetInfo",userPetInfo);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_recommend;
        TextView tv_name;
        TextView tv_nickname;
        TextView tv_postdate;
        CircleImageView iv_portrait;
        LinearLayout ly_item_recommend_recycle_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_recommend=itemView.findViewById(R.id.iv_recommend);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_nickname=itemView.findViewById(R.id.tv_nickname);
            tv_postdate=itemView.findViewById(R.id.tv_pet_location);
            iv_portrait=itemView.findViewById(R.id.iv_portrait);
            ly_item_recommend_recycle_view=itemView.findViewById(R.id.ly_item_recommend_recycle_view);
        }
    }
}
